/*
 * @Author: hfqf123@126.com
 * @Date: 2023-01-08 16:28:06
 * @LastEditors: user.email
 * @LastEditTime: 2023-01-19 15:27:11
 * @FilePath: /spemqtt/client/js/mqtt-manager.js
 * @Description: 
 * 
 * Copyright (c) 2023 by hfqf123@126.com, All Rights Reserved. 
 */




const MQTTEventState = {
    MQTTEvent_default: 0, //初始化状态
    MQTTEvent_getConfigError: 1, //获取配置信息错误
    MQTTEvent_connectError: 2, //各种连接失败
    MQTTEvent_subError: 3, //订阅失败
    MQTTEvent_initSDKSucceed: 4 //sdk启动成功
}

/**
 *该类执行依赖mqtt.min.js
 */
var MQTTManager = (function() {
    var instance_mqtt_manager;
    return function() {
        if (instance_mqtt_manager) {
            return instance_mqtt_manager
        }
        this.httpposter = null
        this.clienter = null
        this.receivedMsgCallBack = null
        this.sdkStateChanedCallback = null
        return instance_mqtt_manager = this
    }
})()



MQTTManager.prototype.initMQTTSDK = function(client, httpposter, onReceivedMsgCallBack, onSDKStateChanedCallback) {
    if (!client) {
        console.log('MQTTManager', 'client must be setted')
        return this;
    }

    if (!httpposter) {
        console.log('MQTTManager', 'httpposter must be setted')
        return this;
    }
    this.clienter = client
    this.httpposter = httpposter
    this.receivedMsgCallBack = onReceivedMsgCallBack
    this.sdkStateChanedCallback = onSDKStateChanedCallback
    client.on('connect', () => {
        console.log('connected.');

        client.subscribe("mee")

        client.on("message", function(topic, payload) {
            console.log(payload);
            console.log([topic, payload].join(": "));
            // client.end()
        });

        client.publish("mee", "hello");
    });
    return this
}

/**
 * 连接mqtt
 */
MQTTManager.prototype.connect = function(client) {
    client.connect()
}

/**
 * 连接mqtt
 */
MQTTManager.prototype.disconnect = function(client) {
    client.disconnect()
}

MQTTManager.prototype.post = function(url, para) {

}